package com.example.DataSource.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.DataSource.Mapper.LoginMapper;
import com.example.DataSource.ServiceImpl.LoginServiceImpl;
import com.example.DataSource.ServiceImpl.consultantServiceImpl;
import com.example.DataSource.ServiceImpl.memberServiceImpl;
import com.example.DataSource.entity.consultant;
import com.example.DataSource.entity.member;
import com.example.DataSource.entity.user;
import com.example.DataSource.entity.userLogin;
import com.example.tokenUtil.TokenResponse;
import com.example.tokenUtil.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/auth")
public class LoginAndRegisterController {
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private LoginServiceImpl loginService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public ResponseEntity<?> login(
            @RequestBody user loginUser
    ) {
        System.out.println(loginUser.toString());
        System.out.println("登录的用户：" + loginUser.getUserName());
        System.out.println("登录的用户的密码：" + loginUser.getPwd());
        QueryWrapper<userLogin> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("UserName", loginUser.getUserName());
//                .eq("pwd", loginUser.getPwd());
        List<userLogin> list = loginService.list(queryWrapper);

        if (!list.isEmpty()) {
            if (list.get(0).getPwd().equals(loginUser.getPwd())) {
                String token = tokenService.generateToken(loginUser.getUserName()); // 生成token
                return ResponseEntity.ok(new TokenResponse("登录成功！", token)); // 返回token
            }
            return ResponseEntity.status(401).body("用户名或密码错误");
        } else {
            return ResponseEntity.status(404).body("用户不存在");
        }
    }

    @PostMapping("/register")
    public String register(@RequestBody user registerUser) {
        return loginService.register(registerUser);
    }


    @Autowired
    private consultantServiceImpl consultantService;
    @Autowired
    private memberServiceImpl memberService;

    @PostMapping("/getInfo")
    public ResponseEntity<?> getInfo(
            @RequestBody user loginUser
    ) {
        if (loginUser.getType() == 0) {
//            获取前台会员消息
            QueryWrapper<member> memberQueryWrapper = new QueryWrapper<>();
            memberQueryWrapper
                    .eq("username", loginUser.getUserName())
                    .eq("password", loginUser.getPwd());
            List<member> list = memberService.list(memberQueryWrapper);
            if (!list.isEmpty()){
                member member = list.get(0);
                return ResponseEntity.ok(member);
            }else {
                return ResponseEntity.status(401).body("用户信息不存在");
            }
        } else if (loginUser.getType() == 1) {
//            获取后台咨询师消息
            QueryWrapper<consultant> consultantQueryWrapper = new QueryWrapper<>();
            consultantQueryWrapper
                    .eq("username", loginUser.getUserName())
                    .eq("password", loginUser.getPwd());
            List<consultant> list = consultantService.list(consultantQueryWrapper);
            if (!list.isEmpty()){
                consultant consultant = list.get(0);
                return ResponseEntity.ok(consultant);
            }else{
                return ResponseEntity.status(401).body("用户信息不存在");
            }
        } else {
            return null;
        }
    }
}
